$(function () {
    get_goods_info(0);
});

/**
 * 获取仓储商品信息
 * @param page 页码
 */
function get_goods_info(page) {
    $("#state").val(0);
    $("#repertory_good").show();
    $("#repertory_indent").hide();
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/shopindent.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            page: page
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                var datas = data.data;
                var goodinfo = "";
                $(datas).each(function () {
                    goodinfo += '<div class="item" >' +
                        '<div class="left">' +
                        '<input id="' + this.repertory_id + '" onclick="chose_radio(\'' + this.repertory_id + '\', true);" type="radio" class="radio_left" />' +
                        '</div>' +
                        '<input type="hidden" id="shopid' + this.repertory_id + '" value="' + this.shop_id + '">' +
                        '<input type="hidden" id="specpriceid' + this.repertory_id + '" value="' + this.shop_spec_price + '">' +
                        '<div class="right">' +
                        '<div class="goods">' +
                        '<img src="' + this.img + '" class="item_img" onerror="imgerror(this,\'img/onerror.png\');"/>' +
                        '<ul class="item_ul">' +
                        '<li><span class="item_name">' + this.shop_name + '</span></li>' +
                        '<li>' +
                        '<div style="margin-top: 10px;">' +
                        '<span class="item_desc">' + this.name + '</span>' +


                        '<div class="count">'+
                        '<div class="add_div">'+
                        '<div onclick="minus(\'' + this.repertory_id + '\')">'+
                        '<span>-</span>'+
                        '</div>'+
                        '<div>'+
                        '<span id="number' + this.repertory_id + '">' + this.surplusnum + '</span>'+
                        '</div>'+
                        '<div onclick="add(\'' + this.repertory_id + '\')">'+
                        '<span>+</span>'+
                        '</div>'+
                        '</div>'+
                        '</div>'+


                        // '<div class="count">' +
                        // '<b onclick="minus(\'' + this.repertory_id + '\')">-</b><input id="number' + this.repertory_id + '" type="number" value="0" style="width: 20px;margin-left: 10px;"/><b onclick="add(\'' + this.repertory_id + '\')">+</b>' +
                        // '</div>' +
                        '</div>' +
                        '</li>' +
                        '<li><span class="item_money">￥:' + this.price + '</span></li>' +
                        '</ul>' +
                        '</div>' +
                        '</div>' +
                        '</div>';
                });
                remove_advertising();
            } else if (code == 20013) {

            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
            if (page == 0)
                $("#repertory_good").empty().append(goodinfo);
            else
                $("#repertory_good").append(goodinfo);
            $("#page").val(page * 1 + 1);
        }
    });
}

/**
 * 点击减号
 * @param repertoryid
 */
function minus(repertoryid) {
    $("#" + repertoryid).attr("checked", true);
    var number = $("#number" + repertoryid).html();
    if (number * 1 <= 1) {
        $("#" + repertoryid).attr("checked", false);
        $("#number" + repertoryid).html(0);
    } else
        $("#number" + repertoryid).html(number * 1 - 1);
}

/**
 * 点击加号
 * @param repertoryid
 */
function add(repertoryid) {
    $("#" + repertoryid).attr("checked", true);
    var number = $("#number" + repertoryid).html();
    $("#number" + repertoryid).html(number * 1 + 1);
}

/**
 * 选择所有
 */
function choseAll() {
    $("#repertory_good :radio").attr("checked", true).attr("onclick", "chose_radio('" + repertoryid + "', false);");
    $("#repertory_good :radio").each(function () {
        var id = $(this).attr("id");
        var number = $("#number" + id).html();
        if (number * 1 <= 1)
            $("#number" + id).html(1);
    });
}

/**
 * 单选按钮点击时间
 * @param repertoryid
 * @param flag
 */
function chose_radio(repertoryid, flag) {
    if (flag) {
        $("#" + repertoryid).attr("checked", true).attr("onclick", "chose_radio('" + repertoryid + "', false);");
        var number = $("#number" + repertoryid).html();
        if (number * 1 <= 1)
            $("#number" + repertoryid).html(1);
    } else
        $("#" + repertoryid).attr("checked", false).attr("onclick", "chose_radio('" + repertoryid + "', true);");
}

var clickfalg = false;

/**
 * 点击生成订单按钮
 */
function add_repertory_indent() {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    if (clickfalg)
        return;
    clickfalg = true;
    var flag = false;
    var shopinfo = "";
    $("#repertory_good :radio").each(function () {
        var checked = $(this).attr("checked");
        var id = $(this).attr("id");
        var number = $("#number" + id).html();
        var shopid = $("#shopid" + id).val();
        var specpriceid = $("#specpriceid" + id).val();
        if (number * 1 >= 1 && checked) {
            flag = true;
            shopinfo += "{shopid:" + shopid + ",num:" + number + ",shopspecid:" + specpriceid + ",repertoryid:" + id + "},";
        }
    });
    if (flag && !isempty(shopinfo)) {
        var addressid = $("#addressid").val();
        shopinfo = "[" + shopinfo.substr(0, shopinfo.length - 1) + "]";
        var jsonstr = "{token:" + token + ",addressids:" + addressid + ",shopinfo: " + shopinfo + "}";
        var url = "/except/indentcontroller/repertoryindent.action";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                json: jsonstr,
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                if (code == 10001 || code == 10002)
                    location.href = "/except/login.jsp";
                else if (code == 20000) {
                    var datas = data.data;
                    location.href = "/except/files/indent/repertory_indent_info.jsp?indentinfo=" + JSON.stringify(datas);
                } else {
                    $("#hint_message").html(message);
                    $("#hintbg").show();
                    clickfalg = false;
                }
            }
        });
    } else {
        $("#hint_message").html("请选择商品，商品数量不能为0");
        $("#hintbg").show();
        clickfalg = false;
    }
}